package com.atguigu.gulimall.seckill.controller;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.seckill.service.SecKillService;
import com.atguigu.gulimall.seckill.to.SkuSecKillCahceTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SeckillController {

    @Autowired
    SecKillService secKillService;

    @GetMapping("/currentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus(){
        List<SkuSecKillCahceTo> curSkus = secKillService.getCurrentSecKillSkus();
        return R.ok().put("data",curSkus);
    }

    @GetMapping("/sku/seckill/{skuId}")
    @ResponseBody
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){
        SkuSecKillCahceTo skuSecKillCahceTo = secKillService.getSkuSeckillInfo(skuId);
        return R.ok().put("data",skuSecKillCahceTo);
    }

    @GetMapping("/kill")
    public String secKill(@RequestParam("killId") String killId,
                     @RequestParam("code") String code,
                     @RequestParam("num") Integer num,
                          Model model){

        String orderSn = secKillService.kill(killId,code,num);
        model.addAttribute("orderSn",orderSn);
        return "success";
    }

}
